// read.php
// noinspection JSUnusedLocalSymbols

import {MyCache} from "@ginstone/common-utils/dist/src/utils/MyCache";
import {ReadApi} from "@ginstone/nga-api/dist/src/method/ReadApi";
import {NukeApi} from "@ginstone/nga-api/dist/src/method/NukeApi";
import {TopicRecommendParam} from "@ginstone/nga-api/dist/src/params/NukeParams";
import {ElementUtils} from "@/assets/js/ElementUtils";

//判断一个回复是否应被屏蔽
const blockMatch = (reply, blockData) => {
    const {keywords, users} = blockData
    if (reply.content) {
        for (let i = 0; i < keywords.length; i++) {
            if (reply.content.includes(keywords[i])) {
                return false;
            }
        }
    }
    //用户id
    const userId = users.map(i => Number(i.split('/')[0]));
    if (reply.authorUid) {
        return !userId.includes(reply.authorUid)
    }

    return true
}

//过滤被屏蔽的回复
const filterReplies = async (dispatch, res) => {
    const blockData = await dispatch("Block/getBlockData", false, {root: true})
    res.blockedReplies = res.replies.filter(i => !blockMatch(i, blockData))
    res.replies = res.replies.filter(i => blockMatch(i, blockData))
    return res;
}

export default {
    namespaced: true, state: {
        cache: new MyCache(),
    }, mutations: {
        method(state, payload) {

        },
        //  处理请求响应
        handleRes(state, res) {
            this.commit("User/updateAccountStatus", res.userContext.userInfo)
        },
    }, actions: {
        method: ({dispatch, commit, state, rootGetters}, payload) => {

        },
        // 点赞或点踩
        topicRecommend: ({dispatch, commit, state, rootGetters}, {topicId, replyId, value}) => {
            const client = rootGetters["Account/getClient"]();
            return NukeApi.topicRecommend(client, new TopicRecommendParam(topicId, replyId, value))
                .then(({message, value}) => {
                    ElementUtils.notice({message})
                    return value;
                })
                .catch(ElementUtils.exceptionHandler)
        },
        //获取版头正文
        readToppedTopic: ({dispatch, commit, state, rootGetters}, {tid, force}) => {
            const client = rootGetters["Account/getClient"]();
            const params = {tid}
            return dispatch("readTopic", {params, force}).then(res => ({title: res.replies[0].title, nodes: res.replies[0].contentNodes}))
        },
        // 网页请求一个主题页
        readTopicDoc: ({dispatch, commit, state, rootGetters}, {params, force}) => {
            const client = rootGetters["Account/getClient"]();
            const method = () => ReadApi.readTopicDoc(client, params).then(res => filterReplies(dispatch, res))
            return state.cache.use({...params, html: true}, method, force).then(res => {
                commit("handleRes", res)
                if (params.page === 'e') {
                    state.cache.remove(params)
                }
                return res;
            })
        },
        // json请求一个主题页
        readTopic: ({dispatch, commit, state, rootGetters}, {params, force}) => {
            const client = rootGetters["Account/getClient"]();
            const method = () => ReadApi.readTopic(client, params).then(res => filterReplies(dispatch, res))

            return state.cache.use(params, method, force).then(res => {
                commit("handleRes", res)
                if (params.page === 'e') {
                    state.cache.remove(params)
                }
                return res;
            }).catch(reason => {
                console.log(reason)
                const {code2, message} = reason
                if (code2 === 500) {
                    console.warn("json请求失败,使用网页请求")
                    return dispatch("readTopicDoc", {params, force})
                }
                throw reason
            })
        },
        // json请求一个回复
        readReply: ({dispatch, commit, state, rootGetters}, {pid, force}) => {
            const params = {pid}
            const client = rootGetters["Account/getClient"]();
            const method = () => ReadApi.readReply(client, pid)
            return state.cache.use(params, method, force).then(res => {
                commit("handleRes", res)
                return res;
            }).catch(reason => {
                const {code2, message} = reason
                if (code2) {
                    console.warn("json请求失败,使用网页请求")
                    return dispatch("readReplyDoc", {pid, force})
                }
                throw reason
            })
        },
        // 网页请求一个回复
        readReplyDoc: ({dispatch, commit, state, rootGetters}, {pid, force}) => {
            const client = rootGetters["Account/getClient"]();
            const method = () => ReadApi.readReplyDoc(client, pid)
            return state.cache.use({pid, html: true}, method, force).then(res => {
                commit("handleRes", res)
                return res;
            })
        },

    }, getters: {},
}